from django.db import models
from ckeditor.fields import RichTextField
from ckeditor_uploader.fields import RichTextUploadingField
# Create your models here.
class BaseModel(models.Model):
    # 模型类抽象基类
    create_time = models.DateTimeField(
        auto_now_add=True,
        verbose_name='创建时间',
    )
    update_time = models.DateTimeField(
        auto_now=True,
        verbose_name='更新时间',
    )

    class Meta:
        abstract = True

class GoodsCategory(BaseModel):
    # 商品类别
    name = models.CharField(max_length=10, verbose_name='名称')
    parent = models.ForeignKey(
        'self',
        null=True,
        blank=True,
        on_delete=models.CASCADE,
        verbose_name='商品父类',
    )

    class Meta:
        db_table = 'tb_goods_category'
        verbose_name_plural = '商品分类'

    def __str__(self):
        return self.name


class GoodsChannle(BaseModel):
    # 商品频道
    group_id = models.IntegerField(
        verbose_name='组号',
    )

    category = models.ForeignKey(
        GoodsCategory,
        on_delete=models.CASCADE,
        verbose_name='顶级商品分类',
    )
    url = models.CharField(
        max_length=50,
        verbose_name='商品页面连接',
    )
    sequence = models.IntegerField(
        verbose_name='顺序'
    )
    class Meta:
        db_table = 'tb_goods_channel'
        verbose_name_plural = '商品频道'

    def __str__(self):
        return self.category.name


class Brand(BaseModel):
    # 品牌
    name = models.CharField(
        max_length=20,
        verbose_name='名称',
    )
    logo = models.ImageField(
        verbose_name='LOGO图片',
    )
    first_letter  = models.CharField(
        max_length=1,
        verbose_name='品牌首字母',
    )

    class Meta:
        db_table = 'tb_brands'
        verbose_name_plural = '商品品牌'

    def __str__(self):
        return self.name


class Goods(BaseModel):
    # 商品SPU，SPU一个产品的类型
    name = models.CharField(
        max_length=50,
        verbose_name='名称',
    )
    brand = models.ForeignKey(
        Brand,
        on_delete=models.PROTECT,  # 删除关联数据，引发ProtectedError，保护关联外键
        verbose_name='品牌',
    )
    category1  = models.ForeignKey(
        GoodsCategory,
        on_delete=models.PROTECT,
        related_name='cat1_goods',
        verbose_name='一级分类'
    )
    category2  = models.ForeignKey(
        GoodsCategory,
        on_delete=models.PROTECT,
        related_name='cat2_goods',
        verbose_name='二级分类'
    )
    category3  = models.ForeignKey(
        GoodsCategory,
        on_delete=models.PROTECT,
        related_name='cat3_goods',
        verbose_name='三级分类'
    )
    sales  = models.IntegerField(
        default=0,
        verbose_name='销量',
    )
    comments  = models.IntegerField(
        default=0,
        verbose_name='评价数',
    )
    # 还有三个字段需要等待富文本编辑器的加入才能完成

    desc_detail = RichTextUploadingField(default='', verbose_name='详细介绍')
    desc_pack = RichTextField(default='', verbose_name='包装信息')
    desc_service = RichTextUploadingField(default='', verbose_name='售后服务')

    class Meta:
        db_table = 'tb_goods'
        verbose_name_plural = '商品SPU属性'

    def __str__(self):
        return self.name


class GoodsSpecification(BaseModel):
    # 商品规格
    goods = models.ForeignKey(
        Goods,
        on_delete=models.CASCADE,
        verbose_name='商品',
    )
    name = models.CharField(
        max_length=20,
        verbose_name='规格分类',
    )
    #mongdb 
    class Meta:
        db_table = 'tb_goods_specification'
        verbose_name_plural = '商品规格'

    def __str__(self):
        return '%s: %s' % (self.goods.name, self.name)


class SpecificationOption(BaseModel):
    # 规格选项
    spec = models.ForeignKey(
        GoodsSpecification,
        on_delete=models.CASCADE,
        verbose_name='规格分类',
    )
    value = models.CharField(
        max_length=20,
        verbose_name='规格选项',
    )

    class Meta:
        db_table = 'tb_specification_option'
        verbose_name_plural = '规格选项'

    def __str__(self):
        return '%s - %s' % (self.spec.name, self.value)


class GoodsSku(BaseModel):
    # 商品SKU属性
    name = models.CharField(max_length=50, verbose_name='名称')
    caption = models.CharField(max_length=100, verbose_name='副标题')
    goods = models.ForeignKey(
        Goods,
        on_delete=models.CASCADE,
        verbose_name='商品',
        related_name='goods_sku',
    )
    category = models.ForeignKey(
        GoodsCategory,
        on_delete=models.PROTECT,
        verbose_name='类别',
    )
    price = models.DecimalField(
        max_digits=10,
        decimal_places=2,
        verbose_name='单价',
    )
    purchase_price = models.DecimalField(
        max_digits=10,
        decimal_places=2,
        verbose_name='进价',
    )
    market_price = models.DecimalField(
        max_digits=10,
        decimal_places=2,
        verbose_name='市场价',
    )
    stock = models.IntegerField(
        default=0,
        verbose_name='库存',
    )
    sales = models.IntegerField(
        default=0,
        verbose_name='销量',
    )
    comments = models.IntegerField(
        default=0,
        verbose_name='评价数量'
    )
    is_launched = models.BooleanField(
        default=True,
        verbose_name='是否上架'
    )
    default_image_url = models.CharField(
        max_length=200,
        default='',
        verbose_name='商品默认图片',
        null=True,
        blank=True,
    )


    #sku1: mac i5 红色
    #sku2: mac i7 灰色
    #sku3: mac i5 灰色
    #sku5: mac i7 红色
    class Meta:
        db_table = 'tb_sku'
        verbose_name_plural = '商品SKU属性'

    def __str__(self):
        return self.name

class SkuImage(BaseModel):
    # sku图片描述
    sku = models.ForeignKey(
        GoodsSku,
        on_delete=models.CASCADE,
        verbose_name='sku',
    )
    image = models.ImageField(verbose_name='图片')

    class Meta:
        db_table = 'tb_sku_image'
        verbose_name_plural = 'SKU图片描述'

    def __str__(self):
        return self.sku.name


class SKUSpecification(BaseModel):
    sku = models.ForeignKey(
        GoodsSku,
        on_delete=models.CASCADE,
        verbose_name='sku',
    )
    spec = models.ForeignKey(
        GoodsSpecification,
        on_delete=models.PROTECT,
        verbose_name='规格分类',
    )
    option = models.ForeignKey(
        SpecificationOption,
        on_delete=models.PROTECT,
        verbose_name='规格选项',
    )

    class Meta:
        db_table = 'tb_sku_specification'
        verbose_name_plural = 'SKU规格'

    def __str__(self):
        # return '%s: %s - %s' % (self.sku.name, self.spec.name, self.option.value)
        return self.sku.name